/*****************************************************************************
* Keppe Messenger (KMP) Keppe Project-Team User Messaging System
* Copyright (C) 2013-2014 Keppe Project-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#include "frmLogin.h"
#include "frmContactRequests.h"
#include "ui_frmContactRequests.h"
#include <QMessageBox>
QMap<QString,QListWidgetItem*> stringLinkRequest;
QMap<QListWidgetItem*,QString> requestLinkString;
QStringList friendRequests;
QObject *formFR;

frmContactRequests::frmContactRequests(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frmContactRequests)
{
    setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
    ui->setupUi(this);
}

void frmContactRequests::setForm(frmLogin* login)
{
    formFR = login;
}

void frmContactRequests::setContacts(QStringList contacts)
{
    ui->lwRequests->clear();
    friendRequests = contacts;
    foreach(const QString& contact, contacts)
    {
        QListWidgetItem *contactItem = new QListWidgetItem(contact);
        contactItem->setIcon(QIcon(":/gui/KeppeMessengerAvatar.png"));
        stringLinkRequest[contact] = contactItem;
        requestLinkString[contactItem] = contact;
        ui->lwRequests->addItem(contactItem);
    }
    if (contacts.length() == 1)
    {
        ui->labFR->setText(QApplication::tr("You have 1 Friend Request"));
    }
    else
    {
        ui->labFR->setText(QApplication::tr("You have %1 Friend Requests").arg(QString::number(contacts.length())));
    }
}

void frmContactRequests::updateNumber(QStringList contacts)
{
    if (contacts.length() == 1)
    {
        ui->labFR->setText(QApplication::tr("You have 1 Friend Request"));
    }
    else
    {
        ui->labFR->setText(QApplication::tr("You have %1 Friend Requests").arg(QString::number(contacts.length())));
    }
}

frmContactRequests::~frmContactRequests()
{
    delete ui;
}

void frmContactRequests::on_cmdClose_clicked()
{
    this->close();
}

void frmContactRequests::on_cmdAccept_clicked()
{
    frmLogin *login = (frmLogin*)formFR;
    if (login->isConnectedWithServer())
    {
        QListWidgetItem *item = ui->lwRequests->currentItem();
        if (requestLinkString.contains(item))
        {
            int ret = login->addUserContact(requestLinkString[item]);
            if (ret == 200)
            {
                login->updateFriendRequestList("",requestLinkString[item]);
                login->addContactItem(requestLinkString[item]);
                friendRequests.removeAll(requestLinkString[item]);
                stringLinkRequest.remove(requestLinkString[item]);
                requestLinkString.remove(item);
                updateNumber(friendRequests);
                delete item;
            }
            else
            {
                QMessageBox::warning(this,QApplication::tr("Accept friend request"),QApplication::tr("Accept friend request failed!"));
            }
        }
    }
    else
    {
        QMessageBox::information(this,QApplication::tr("Offline"),QApplication::tr("You are offline, please wait until you're online!"));
    }
}

void frmContactRequests::on_cmdReject_clicked()
{
    frmLogin *login = (frmLogin*)formFR;
    if (login->isConnectedWithServer())
    {
        QListWidgetItem *item = ui->lwRequests->currentItem();
        if (requestLinkString.contains(item))
        {
            int ret = login->delUserContact(requestLinkString[item]);
            if (ret == 200)
            {
                login->updateFriendRequestList("",requestLinkString[item]);
                friendRequests.removeAll(requestLinkString[item]);
                stringLinkRequest.remove(requestLinkString[item]);
                requestLinkString.remove(item);
                updateNumber(friendRequests);
                delete item;
            }
            else
            {
                QMessageBox::warning(this,QApplication::tr("Reject friend request"),QApplication::tr("Reject friend request failed!"));
            }
        }
    }
    else
    {
        QMessageBox::information(this,QApplication::tr("Offline"),QApplication::tr("You are offline, please wait until you're online!"));
    }
}

void frmContactRequests::on_bbClose_clicked(QAbstractButton *button)
{
    if (ui->bbClose->buttonRole(button) == QDialogButtonBox::RejectRole)
    {
        on_cmdClose_clicked();
    }
}
